package com.ihm.projetihm_v1.activity.main;

import java.io.File;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.AsyncTask;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.ihm.projetihm_v1.activity.main.listener.MainImageListener;
import com.ihm.projetihm_v1.activity.photo.listener.ImageControlListener;

/**
 * use to download images with performance, for listview
 * 
 * @author Adrien
 *
 */
public class ImageDownloader
{
	//++++++++++++//
	// ATTRIBUTES //
	//++++++++++++//
	
	private ViewGroup mImagesContainer;
	
	private int mMaxWidth;
	
	private int mMaxHeight;
	
	private boolean isMainActivity;

	//++++++++++++++//
	// CONSTRUCTORS //
	//++++++++++++++//
	
	public ImageDownloader(
			ViewGroup imagesContainer,
			int maxWidth,
			int maxHeight)
	{
		this.mImagesContainer = imagesContainer;
		this.mMaxWidth = maxWidth;
		this.mMaxHeight = maxHeight;
	}
	

	//+++++++++//
	// METHODS //
	//+++++++++//
	
	/**
	 * 
	 * fill the image view with the good image
	 * @param photo
	 */
	public void download(File photo, boolean isMainActivity)
	{
		BitmapDownloaderTask task =
				new BitmapDownloaderTask(photo);
		this.isMainActivity = isMainActivity;
		task.execute(photo.getAbsolutePath());
	}
	
	
	//+++++++++++++++//
	// INNER CLASSES //
	//+++++++++++++++//
	
	/**
	 * permit to download bitmap in imageview
	 * @author Adrien
	 *
	 */
	private class BitmapDownloaderTask
			extends AsyncTask<String, Void, Bitmap>
	{
		private File mPhoto;

		public BitmapDownloaderTask(File photo)
		{
			this.mPhoto = photo;
		}
		
		@Override
		protected void onPreExecute()
		{
		}
		
		@Override
		protected Bitmap doInBackground(String... params)
		{
			String filePath = params[0];
			
			return getSampleBitmapFromMaxSize(
					filePath,
		    		ImageDownloader.this.mMaxWidth,
		    		ImageDownloader.this.mMaxHeight
					);
		}
		
		@Override
		protected void onPostExecute(Bitmap result)
		{
			Context context = ImageDownloader.this.mImagesContainer.getContext();
        	ImageButton imageView = new ImageButton(context);
        	imageView.setImageBitmap(result);
        	ImageDownloader.this.mImagesContainer.addView(imageView);
        	
			if(ImageDownloader.this.isMainActivity){	
				imageView.setOnClickListener(
						new MainImageListener(
								context,
								this.mPhoto
							)
					);
			}
			else 
			{
				imageView.setOnClickListener(
						new ImageControlListener(
								context,
								this.mPhoto
							)
					);
			}
		
		}
	}

	public static Bitmap getSampleBitmapFromMaxSize(
			String filePath,
			int maxWidth,
			int maxHeight)
	{
		Options options = new Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(
				filePath,
				options
				);
		// Calculate inSampleSize
	    options.inSampleSize = calculateInSampleSize(
	    		options,
	    		maxWidth,
	    		maxHeight);
	    
	    // Decode bitmap with inSampleSize set
	    options.inJustDecodeBounds = false;

		return BitmapFactory.decodeFile(
				filePath,
				options
				);
	}
	
	private static int calculateInSampleSize(
            Options options,
            int maxWidth,
            int maxHeight)
	{
	    // Raw height and width of image
	    final int height = options.outHeight;
	    final int width = options.outWidth;
	    int inSampleSize = 1;
	    if (height > maxHeight || width > maxWidth)
	    {
			// Calculate ratios of height and width to requested height and width
			final int heightRatio = (int) Math.ceil((float) height / (float) maxHeight);
			final int widthRatio = (int) Math.ceil((float) width / (float) maxWidth);
			
			// Choose the smallest ratio as inSampleSize value, this will guarantee
	        // a final image with both dimensions larger than or equal to the
	        // requested height and width.
	        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    	}
	    return inSampleSize;
	}
	
	/**
	 * 
	 * @param filePath
	 * @param maxWidth more than 15
	 * @param maxHeight more than 15
	 * @return
	 */
	public static Bitmap getScaledBitmapFromMaxSize(
			String filePath,
			int maxWidth,
			int maxHeight)
	{
		if (maxWidth<16 || maxHeight<16)
		{
			return null;
		}
		Bitmap bm = BitmapFactory.decodeFile(
				filePath,
				null
				);
		int height = bm.getHeight();
		int width = bm.getWidth();
		if (height > maxHeight || width > maxWidth)
		{
		    float scaleWidth = (float)width/(float)maxWidth;
		    float scaleHeight = (float)height/(float)maxHeight;
		    if (scaleWidth < scaleHeight)
		    {
			    width = (int) ((float)width / scaleHeight);
			    height = (int) ((float)height / scaleHeight);
		    }
		    else
		    {
			    width = (int) ((float)width / scaleWidth);
			    height = (int) ((float)height / scaleWidth);
		    }
		    bm = Bitmap.createScaledBitmap(bm, width, height, false);
		}
	    return bm;
	}
}
